﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using TaoPortal.BusinessLogic.DTO;
using TaoPortal.BusinessLogic.Extensions;
using TaoPortal.BusinessLogic.Repository;
using TaoPortal.Entity;


namespace TaoPortal.Areas.Admin.Models
{
    public class SimplePageModel : BaseModel
    {
        public List<System.Web.Mvc.SelectListItem> StatusList { get; set; }
        public SimplePageSearcher Searcher { get; set; }
        public AddSimplePage AddModel { get; set; }

        public SimplePageModel()
        {
            Searcher = new SimplePageSearcher();
        }

        public PagedList<SimplePageList> GetSimplePageList()
        {
            var rp = new SimplePageRepository();
            var data = rp.GetSimplePageList(Searcher.PTitle, Searcher.PKey, Searcher.StartDate, Searcher.EndDate, PageIndex, PageSize);
            return data;
        }

        public void Add()
        {
            //需要生成页面并返回URL
            string url = string.Empty;
            var rp = new SimplePageRepository();
            rp.AddSimplePage(new TP_SimplePage
            {
                CreateBy = SessionEnt.UserName,
                CreateTime = DateTime.Now,
                PKey = AddModel.PKey,
                PUrl = url,
                PBody = AddModel.PBody,
                PTitle = AddModel.PTitle,
                UpdateBy = SessionEnt.UserName,
                UpdateTime = DateTime.Now
            });
        }

        public void Edit()
        {
            var rp = new SimplePageRepository();
            rp.UpdateSimplePage(new TP_SimplePage
            {
                ID = AddModel.ID,
                UpdateBy = SessionEnt.UserName,
                UpdateTime = DateTime.Now,
                PTitle = AddModel.PTitle,
                PKey = AddModel.PKey,
                PBody = AddModel.PBody
            });
        }

        public void DeleteSimplePage(string id)
        {

            //需要删除文件
            var rp = new SimplePageRepository();
            rp.DeleteSimplePage(id);
        }

        public void InitSimplePage(string id)
        {
            var rp = new SimplePageRepository();
            var sp = rp.GetSimplePageByID(id);
            AddModel = new AddSimplePage
            {
                ID = sp.ID,
                PTitle = sp.PTitle,
                PBody = sp.PBody,
                PKey = sp.PKey
            };
        }
    }


    public class SimplePageSearcher
    {
        public string PTitle { get; set; }
        public string PKey { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
    }

    public class AddSimplePage
    {
        public string ID { get; set; }
        [Required]
        [Display(Name = "标题")]
        public string PTitle { get; set; }
        [Required]
        [Display(Name = "关键字")]
        public string PKey { get; set; }
        [Required]
        [Display(Name = "内容")]
        public string PBody { get; set; }
    }
}